/**
 * Created by Administrator on 2017/7/30.
 */
(function ($, window) {
    var linkage = {
        _this: this,
        $material: $('#material'),
        $book: $('#book'),
        $lesson: $('#lesson'),
        clearBook: clearBook,
        clearLesson: clearLesson,
        clearAll: function () {
            this.clearBook(_this);
            this.clearLesson(_this);
        },
        getBooks: function () {
            getBooks(_this, this.fillBooks);
        },
        changeMaterial: function () {
            _this = this;
            this.$material.change(function () {
                _this.clearAll();
                _this.getBooks();
            })
        },

        getLessons: function () {
            getLessons(_this, this.fillLessons);
        },

        changeBook: function () {
            _this = this;
            this.$book.change(function () {
                _this.clearLesson(_this);
                _this.getLessons();
            })
        },

        fillBooks: function (data) {
            fillBooks(_this, data);
        },

        fillLessons: function (data) {
            fillLessons(_this, data);
        },

        init: function () {
            this.changeMaterial(this._this);
            this.changeBook(this._this);
        }
    };

    function clearBook(e) {
        e.$book.children().remove();
    }

    function clearLesson(e) {
        e.$lesson.children().remove();
    }

    function getLessons(e, fnSuccess) {
        var book = e.$book.val()[0];
        $.ajax({
            url: window.origin + '/getLessonsByBook',
            type: 'GET',
            dataType: 'json',
            data: {
                book: book
            },
            success: function (data) {
                fnSuccess(data);
            },

            error: ajaxError
        })
    }

    function getBooks(e, fnSuccess) {
        var material = e.$material.val()[0];

        console.log(material);
        $.ajax({
            url: window.origin + '/getBooksByMaterial',
            type: 'GET',
            dataType: 'json',
            data: {
                material: material
            },
            success: function (data) {
                fnSuccess(data);
            },

            error: ajaxError
        })
    }

    function fillBooks(e, data) {
        console.log(data);
        if (data.code === 1) {
            var books = data.data;
            var s = '';
            for (var i = 0; i < books.length; i++) {
                s += createOption(books[i]['id'], books[i]['book']);
            }
            e.$book.append($(s));
        }
    }

    function fillLessons(e, data) {
        console.log(data);
        if (data.code === 1) {
            var lessons = data.data;
            var s = '';
            for (var i = 0; i < lessons.length; i++) {
                s += createOption(lessons[i]['id'], lessons[i]['lesson_zh']);
            }
            e.$lesson.append($(s));
        }
    }

    function createOption(val, txt) {
        return '<option value="' + val + '">' + txt + '</option>';
    }

    function ajaxError() {

    }

    window.linkage = linkage;
})(jQuery, typeof window !== 'undefined' ? window : this);